home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / iritfltr / skeletn1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  7.8 KB  |  272 lines

  1. /*****************************************************************************
  2. * Skeleton for an interface to a parser to read IRIT data files.         *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 1.0, Feb 1993    *
  5. *****************************************************************************/
  6.  
  7.  
  8. #ifdef USE_VARARGS
  9. #include <varargs.h>
  10. #else
  11. #include <stdarg.h>
  12. #endif /* USE_VARARGS */
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #include <math.h>
  16. #include <string.h>
  17. #include "irit_sm.h"
  18. #include "iritprsr.h"
  19. #include "attribut.h"
  20. #include "allocate.h"
  21. #include "getarg.h"
  22. #include "genmat.h"
  23. #include "ffcnvrt.h"
  24. #include "ip_cnvrt.h"
  25.  
  26. static MatrixType CrntViewMat;            /* This is the current view! */
  27. static int
  28.     GlblFineNess = 10,
  29.     FourPerFlat = FALSE,
  30.     ReadOneObject = FALSE;
  31.  
  32. static void PrintData(IPObjectStruct *PObj, int Indent);
  33. #ifdef USE_VARARGS
  34. static void IFprintf(int Indent, char *va_alist, ...);
  35. #else
  36. static void IFprintf(int Indent, char *Format, ...);
  37. #endif /* USE_VARARGS */
  38. static char *Real2Str(RealType R);
  39. static void SkelExit(int ExitCode);
  40.  
  41. /*****************************************************************************
  42. * Main routine - read command line parameters and does what is needed...     *
  43. *****************************************************************************/
  44. void main(int argc, char **argv)
  45. {
  46.     IPObjectStruct *PObjects, *PObj;
  47.  
  48.     while (argc >= 2 && argv[1][0] == '-') {
  49.     if (strcmp(argv[1], "-f") == 0) {
  50.         /* Surface to polygon approx. is controlled by GlblFineNess. */
  51.         GlblFineNess = atoi(argv[2]);
  52.         argv += 2;
  53.         argc -= 2;
  54.     }
  55.     else if (strcmp(argv[1], "-4") == 0) {
  56.         /* Create four triangle per flat instead of two in srf to polys. */
  57.         FourPerFlat = TRUE;
  58.         argv++;
  59.         argc--;
  60.     }
  61.     else if (strcmp(argv[1], "-1") == 0) {
  62.         /* Read one object at a time. */
  63.         ReadOneObject = TRUE;
  64.         argv++;
  65.         argc--;
  66.     }
  67.     else {
  68.         IritFatalError("Undefined command line option.\n");
  69.     }
  70.     }
  71.  
  72.     /* Get the data files: */
  73.     if ((PObjects = IritPrsrGetDataFiles(argv + 1, argc - 1, TRUE, FALSE)) ==
  74.                                     NULL)
  75.     SkelExit(1);
  76.  
  77.     if (IritPrsrWasPrspMat)
  78.     MatMultTwo4by4(CrntViewMat, IritPrsrViewMat, IritPrsrPrspMat);
  79.     else
  80.     GEN_COPY(CrntViewMat, IritPrsrViewMat, sizeof(MatrixType));
  81.  
  82.     printf("[OBJECT ALL\n");
  83.     for (PObj = PObjects; PObj != NULL; PObj = PObj -> Pnext)
  84.     PrintData(PObj, 4);
  85.     printf("]\n");
  86. }
  87.  
  88. /*****************************************************************************
  89. * Routine to convert all surfaces into polygons as follows:             *
  90. *****************************************************************************/
  91. IPObjectStruct *IritPrsrProcessFreeForm(IPObjectStruct *CrvObjs,
  92.                     IPObjectStruct *SrfObjs)
  93. {
  94.     CagdSrfStruct *Srf, *Srfs;
  95.     IPObjectStruct *PObj;
  96.     IPPolygonStruct *PPolygon, *PPolygonTemp;
  97.  
  98.     if (SrfObjs) {
  99.         for (PObj = SrfObjs; PObj != NULL; PObj = PObj -> Pnext) {
  100.         Srfs = PObj -> U.Srfs;
  101.         PObj -> U.Pl = NULL;
  102.         PObj -> ObjType = IP_OBJ_POLY;
  103.         IP_SET_POLYGON_OBJ(PObj);
  104.  
  105.         for (Srf = Srfs; Srf != NULL; Srf = Srf -> Pnext) {
  106.             PPolygon = PPolygonTemp =
  107.             IritSurface2Polygons(Srf, FourPerFlat, GlblFineNess, TRUE);
  108.         while (PPolygonTemp -> Pnext)
  109.             PPolygonTemp = PPolygonTemp -> Pnext;
  110.         PPolygonTemp -> Pnext = PObj -> U.Pl;
  111.         PObj -> U.Pl = PPolygon;
  112.         }
  113.         CagdSrfFreeList(Srfs);
  114.     }
  115.     }
  116.     
  117.     return SrfObjs;
  118. }
  119.  
  120. /*****************************************************************************
  121. * ALL FUNCTIONS BELOW ARE FOR PRINTING THE DATA STRUCTURE IN IRIT FORMAT     *
  122. *****************************************************************************/
  123.  
  124. /*****************************************************************************
  125. * Routine to print the data from given geometry object.                 *
  126. *****************************************************************************/
  127. static void PrintData(IPObjectStruct *PObj, int Indent)
  128. {
  129.     int i;
  130.     char *UVVals;
  131.     IPPolygonStruct *PPolygon;
  132.     IPVertexStruct *PVertex;
  133.     IPAttributeStruct
  134.     *Attrs = AttrTraceAttributes(PObj -> Attrs);
  135.  
  136.     if (Attrs != NULL) {
  137.     IFprintf(Indent, "[OBJECT\n");
  138.  
  139.     IFprintf(Indent + 4, "%s\n", Attr2String(Attrs));
  140.  
  141.     IFprintf(0, " %s\n", strlen(PObj -> Name) ? PObj -> Name : "NONE");
  142.     }
  143.     else
  144.         IFprintf(Indent, "[OBJECT %s\n",
  145.          strlen(PObj -> Name) ? PObj -> Name : "NONE");
  146.  
  147.     Indent += 4;
  148.  
  149.     switch (PObj -> ObjType) {
  150.     case IP_OBJ_POLY:
  151.         for (PPolygon = PObj -> U.Pl;
  152.          PPolygon != NULL;
  153.          PPolygon = PPolygon -> Pnext) {
  154.         if (PPolygon -> PVertex == NULL) {
  155.             fprintf(stderr,"Dump: Attemp to dump empty polygon, exit\n");
  156.             SkelExit(1);
  157.         }
  158.         for (PVertex = PPolygon -> PVertex -> Pnext, i = 1;
  159.              PVertex != PPolygon -> PVertex && PVertex != NULL;
  160.              PVertex = PVertex -> Pnext, i++);
  161.         IFprintf(Indent, "[%s %d\n",
  162.              IP_IS_POLYGON_OBJ(PObj) ? "POLYGON" : "POLYLINE", i);
  163.  
  164.         PVertex = PPolygon -> PVertex;
  165.         do {             /* Assume at least one edge in polygon! */
  166.             if ((UVVals = AttrGetStrAttrib(PVertex -> Attrs, "uvvals"))
  167.                                     == NULL)
  168.  
  169.             IFprintf(Indent + 4,
  170.                  "[[NORMAL %s %s %s] %s %s %s]\n",
  171.                  Real2Str(PVertex -> Normal[0]),
  172.                  Real2Str(PVertex -> Normal[1]),
  173.                  Real2Str(PVertex -> Normal[2]),
  174.                  Real2Str(PVertex -> Coord[0]),
  175.                  Real2Str(PVertex -> Coord[1]),
  176.                  Real2Str(PVertex -> Coord[2]));
  177.             else
  178.                 IFprintf(Indent + 4,
  179.                  "[[NORMAL %s %s %s] [UV %s] %s %s %s]\n",
  180.                  Real2Str(PVertex -> Normal[0]),
  181.                  Real2Str(PVertex -> Normal[1]),
  182.                  Real2Str(PVertex -> Normal[2]),
  183.                  UVVals,
  184.                  Real2Str(PVertex -> Coord[0]),
  185.                  Real2Str(PVertex -> Coord[1]),
  186.                  Real2Str(PVertex -> Coord[2]));
  187.  
  188.             PVertex = PVertex -> Pnext;
  189.         }
  190.         while (PVertex != PPolygon -> PVertex && PVertex != NULL);
  191.         IFprintf(Indent, "]\n");           /* Close the polygon. */
  192.         }
  193.         break;
  194.     default:
  195.         IFprintf(Indent + 4, "Non polygonal data is ignored.\n");
  196.         break;
  197.     }
  198.  
  199.     Indent -= 4;
  200.     IFprintf(Indent, "]\n");                /* Close the object. */
  201. }
  202.  
  203. /*****************************************************************************
  204. * Same as fprintf but with indentation.                         *
  205. *****************************************************************************/
  206. #ifdef USE_VARARGS
  207. static void IFprintf(int Indent, char *va_alist, ...)
  208. {
  209.     char *Format, Line[LINE_LEN_LONG];
  210.     int i;
  211.     va_list ArgPtr;
  212.  
  213.     va_start(ArgPtr);
  214.     Format = va_arg(ArgPtr, char *);
  215. #else
  216. static void IFprintf(int Indent, char *Format, ...)
  217. {
  218.     char Line[LINE_LEN_LONG];
  219.     int i;
  220.     va_list ArgPtr;
  221.  
  222.     va_start(ArgPtr, Format);
  223. #endif /* USE_VARARGS */
  224.  
  225.     for (i = 0; i < Indent; i++)
  226.     Line[i] = ' ';
  227.     vsprintf(&Line[Indent], Format, ArgPtr);
  228.     va_end(ArgPtr);
  229.  
  230.     printf(Line);
  231. }
  232.  
  233. /*****************************************************************************
  234. * Convert a real number into a string.                         *
  235. * The routine maintains 6 different buffers simultanuously so up to 6 calls  *
  236. * can be issued from same printf...                         *
  237. *****************************************************************************/
  238. static char *Real2Str(RealType R)
  239. {
  240.     static int j, k,
  241.     i = 0;
  242.     static char Buffer[6][LINE_LEN_SHORT];
  243.  
  244.     if (ABS(R) < EPSILON)
  245.     R = 0.0;                /* Round off very small numbers. */
  246.  
  247.     sprintf(Buffer[i], "%6lg", R);
  248.  
  249.     for (k = 0; !isdigit(Buffer[i][k]) && k < LINE_LEN; k++);
  250.     if (k >= LINE_LEN) {
  251.     fprintf(stderr, "Conversion of real number (%f) failed.\n", R);
  252.     SkelExit(1);
  253.     }
  254.  
  255.     for (j = strlen(Buffer[i]) - 1; Buffer[i][j] == ' ' && j > k; j--);
  256.     if (strchr(Buffer[i], '.') != NULL)
  257.     for (; Buffer[i][j] == '0' && j > k; j--);
  258.     Buffer[i][j+1] = 0;
  259.  
  260.     j = i;
  261.     i = (i + 1) % 6;
  262.     return Buffer[j];
  263. }
  264.  
  265. /*****************************************************************************
  266. * Skeleton exit routine.                              *
  267. *****************************************************************************/
  268. static void SkelExit(int ExitCode)
  269. {
  270.     exit(ExitCode);
  271. }
  272.